/*
	Copyright (C) 2019 Zongming Liu <1849059316@qq.com>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/


#include "TrianglesCommand.h"
#include "../base/Debuger.h"

namespace SkyEngine2d
{
	
	TrianglesCommand::TrianglesCommand()
		:RenderCommand(Type::TrianglesCommand)
		, m_material(nullptr)
	{
	
	}
	
	TrianglesCommand::~TrianglesCommand()
	{
	
	}
	
	
	void TrianglesCommand::use(IDirectRender* d3d_interface)
	{
		if (d3d_interface)
		{
			auto contex = d3d_interface->getD3dDeviceContext();
			m_material->use(contex);
		}
		else
		{
			DebugWarning("d3d �豸�ӿ�Ϊnull ");
		}
	}
	
	int TrianglesCommand::getVertexCount() const
	{
		return m_vertexBuffer.size();
	}
	
	int TrianglesCommand::getIndexCount() const
	{
		return m_indexBuffer.size();
	}
	
	Material & TrianglesCommand::getMaterial()
	{
		return *m_material.get();
	}
	
	
	Matrix4X4 & TrianglesCommand::getMatrix()
	{
		return m_mat;
	}
	
	void TrianglesCommand::init(std::shared_ptr<Material>& material, const Matrix4X4& mat, const VertexVector& vertexBuffer, const IndexVector& indexBuffer)
	{
		m_material = material;
		m_originalVertexBuffer = vertexBuffer;
		m_vertexBuffer = vertexBuffer;
		m_indexBuffer = indexBuffer;
		m_mat = Matrix4X4();
		updataMat(mat);
	}

	void TrianglesCommand::updataMat(const Matrix4X4& mat)
	{
		if (m_mat == mat)
		{
			return;
		}
	
		m_mat = mat;
	
		for (int i = 0; i < m_originalVertexBuffer.size(); i++)
		{
			m_mat.transformVector(m_originalVertexBuffer[i].position, &m_vertexBuffer[i].position);
		}
	}
	
	void TrianglesCommand::updataColor(const Color4f& color)
	{
		for (int i =0;i<m_originalVertexBuffer.size();i++)
		{
			m_originalVertexBuffer[i].color = color;
			m_vertexBuffer[i].color = color;
		}
	}

	const VertexVector& TrianglesCommand::getVertexBuffer() const
	{
		return m_vertexBuffer;
	}
	
	const IndexVector & TrianglesCommand::getIndexBuffer() const
	{
		return m_indexBuffer;
	}
	
}
